package leetcode_5000;

public class _有序数组中的缺失元素_ {
	public static void main(String[] args) {
		_有序数组中的缺失元素_ test = new _有序数组中的缺失元素_();
		System.out.println(test.missingElement(new int[] {4,7,9,10}, 3));
		System.out.println(test.missingElement(new int[] {1,2,4}, 3));
		System.out.println(test.missingElement(new int[] {4,7,9,10}, 1));
	}
	
	public int missingElement(int[] nums, int k) {
        for(int i=1;i<nums.length;++i) {
        	int diff = nums[i]-nums[i-1]-1;
        	if(k > diff) k -= diff;
        	else return nums[i-1]+k;
        }
        return nums[nums.length-1]+k;
    }
}
